<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
  import {add, getDetail, getList, remove, update} from "@/api/app/huodong";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          viewBtn: true,
          addBtn:false,
          editBtn:false,
          delBtn:false,
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: false,
          selection: false,
          dialogClickModal: false,
          column: [
            {
              hide:true,
              label: "活动编号",
              prop: "id",
              // addDisabled: true,
              editDisabled: true,
              editDisplay: false,
              addDisplay: false,
            },
            // {
            //   label: "活动标题",
            //   prop: "title",
            //   rules: [{
            //     required: true,
            //     message: "请输入活动标题",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "活动ID",
            //   prop: "hdcode",
            //   rules: [{
            //     required: true,
            //     message: "请输入活动ID",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "活动名称",
              prop: "hdname",
              search: true,
              rules: [{
                required: true,
                message: "请输入活动名称",
                trigger: "blur"
              }]
            },
            {
              label: "负责人",
              prop: "manager",
              rules: [{
                required: true,
                message: "负责人",
                trigger: "blur"
              }]
            },
            {
              label: "联系电话",
              prop: "phone",
              rules: [{
                required: true,
                message: "联系电话",
                trigger: "blur"
              }]
            },
            {
              label: "开始时间",
              prop: "stime", rules: [{
                required: true,
                message: "开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "结束时间",
              prop: "etime",
              rules: [{
                required: true,
                message: "结束时间",
                trigger: "blur"
              }]
            },
            {
              label: "预计支出",
              prop: "pmoney",
              rules: [{
                required: true,
                message: "预计支出",
                trigger: "blur"
              }]
            },
            // {
            //   label: "区ID",
            //   prop: "areacode",
            //   rules: [{
            //     required: true,
            //     message: "请输入区ID",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "区名称",
              prop: "areaname",
              rules: [{
                required: true,
                message: "请输入区名称",
                trigger: "blur"
              }]
            },
            // {
            //   label: "街道code",
            //   prop: "jdcode",
            //   rules: [{
            //     required: true,
            //     message: "请输入街道code",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "街道名称",
              prop: "jdname",
              rules: [{
                required: true,
                message: "请输入街道名称",
                trigger: "blur"
              }]
            },
            {
              label: "详细地址",
              prop: "address",
              rules: [{
                required: true,
                message: "详细地址",
                trigger: "blur"
              }]
            },
            // {
            //   label: "经纬度",
            //   prop: "jwd",
            //   rules: [{
            //     required: true,
            //     message: "请输入详细地址 集合点/输入",
            //     trigger: "blur"
            //   }]
            // },

            {
              hide:true,
              label: "团队id",
              prop: "teamid",
              rules: [{
                required: true,
                message: "请输入团队id",
                trigger: "blur"
              }]
            },
            {
              label: "团队名称",
              prop: "teamname",
              rules: [{
                required: true,
                message: "请输入团队名称",
                trigger: "blur"
              }]
            },
            {
              label: "参与人数",
              prop: "hduserids",
            },
            {"prop": "fmurl", "label": "封面图", "type": "upload", "listType": "picture-img"},
//            {"prop": "hdqrcode", "label": "签到二维码", "type": "upload", "listType": "picture-img"},
            {
              label: "创建时间",
              prop: "ctime",
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.huodong_add, false),
          viewBtn: this.vaildData(this.permission.huodong_view, false),
          delBtn: this.vaildData(this.permission.huodong_delete, false),
          editBtn: this.vaildData(this.permission.huodong_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
